<template>
  <el-scrollbar :wrap-class="{ wrapClass: true }">
    <div class="app-container">
      <div class="filter-container">
        <el-button
          size="small"
          class="filter-item right"
          icon="el-icon-refresh"
          type="primary"
          plain
          @click="refresh"
        >
          {{ $t('common.refresh') }}
        </el-button>
        <div style="clear: both"></div>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        :height="
          listQuery.total > 0 ? 'calc(100vh - 205px)' : 'calc(100vh - 170px)'
        "
        :header-cell-style="{ background: '#F2F6FC' }"
        element-loading-text="Loading"
        fit
        border
        size="medium"
      >
        <el-table-column
          align="center"
          :label="$t('common.id')"
          width="50"
          type="index"
        />
        <el-table-column
          :label="$t('common.name')"
          min-width="200px"
          prop="name"
        >
        </el-table-column>
        <el-table-column :label="$t('common.status')" width="80" align="center">
          <template slot-scope="{ row }">
            <strong class="">{{ row.stateText }}</strong>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('social.clicked')"
          align="center"
          prop="clickCount"
        >
        </el-table-column>
        <el-table-column
          :label="$t('social.replyNum')"
          align="center"
          prop="replyCount"
        >
        </el-table-column>
        <el-table-column
          :label="$t('social.retweetNum')"
          align="center"
          prop="shareCount"
        >
        </el-table-column>
        <el-table-column
          :label="$t('email.likedNum')"
          align="center"
          prop="likeCount"
        >
        </el-table-column>
        <el-table-column
          :label="$t('common.createTime')"
          width="160px"
          align="center"
          prop="createdAt"
        >
        </el-table-column>
        <el-table-column
          align="center"
          fixed="right"
          :label="$t('common.actions')"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              plain
              icon="el-icon-edit"
              @click="restoreRow(scope.row.id)"
              >{{ $t('social.restore') }}
            </el-button>

            <el-popconfirm
              :title="$t('social.confirmDelete')"
              @confirm="deleteRow(scope.row.id)"
              style="margin-left: 10px"
            >
              <el-button
                type="danger"
                size="mini"
                plain
                icon="el-icon-delete"
                slot="reference"
                >{{ $t('common.delete') }}
              </el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination
        v-show="listQuery.total > 0"
        :total="listQuery.total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="fetchData"
      />
    </div>
  </el-scrollbar>
</template>

<script lang="ts">
import Pagination from '@/components/Pagination/index.vue'
import { getAction, postAction, deleteAction } from '@/api/common'
export default {
  name: 'index',
  components: {
    Pagination,
  },
  data() {
    return {
      list: [],
      listLoading: false,
      listQuery: {
        page: 1,
        total: 0,
        limit: 20,
      },
      selectId: 0,
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      this.listLoading = true
      const { data } = await getAction(
        this.listQuery,
        'socialMarketingRecycleBin'
      )
      this.list = data.items
      this.listQuery.total = data.total
      this.listLoading = false
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    async restoreRow(id: number) {
      const res: any = await postAction(null, `socialMarketingRecycleBin/${id}`)
      if (res.code === 20000) {
        this.$message.success(this.$t('social.restoreSuccessfully'))
      } else {
        this.$message.error(res.message)
      }
      this.refresh()
    },
    async deleteRow(id: number) {
      const res: any = await deleteAction(
        null,
        `socialMarketingRecycleBin/${id}`
      )
      if (res.code === 20000) {
        this.$message.success(this.$t('common.deleteSuccessfully'))
      } else {
        this.$message.error(res.message)
      }
      this.refresh()
    },
    refresh() {
      this.fetchData()
    },
  },
}
</script>

<style scoped></style>
